<template>
  <div class="w100 h100 t-nps-wrap">
    <div class="t-nps-box">
      <span
        v-for="n in createArray(table?.min == undefined ? 1 : table?.min, table?.level)"
        class="item"
        :key="n"
        @mousemove="handleNpsMove(n)"
        @click="handleClick(n)"
        :class="[changeValue === n ? 'active' : '']"
        :style="getHoverStyle(n)"
      >
        {{ n }}
      </span>
    </div>
    <div class="tip-text">
      <div>
        {{ table?.copyWriting.min }}
      </div>
      <div
        v-if="table?.copyWriting.middle"
        class="middle-text"
      >
        {{ table?.copyWriting.middle }}
      </div>
      <div>
        {{ table?.copyWriting.max }}
      </div>
    </div>
  </div>
</template>

<script lang="ts" name="TNps" setup>
import { computed, onMounted, ref, toValue } from "vue";
import { formEmits, formItemProps, useFormItem } from "@/views/formgen/components/FormItem/hooks/useFormItemHook";
import { TinyColor } from "@ctrl/tinycolor";

const props = defineProps({
  ...formItemProps,
  table: {
    type: Object,
    default: () => {}
  }
});

const emits = defineEmits(formEmits);

const formItemHook = useFormItem(props, emits);

const { changeValue, disabledProp } = formItemHook;

const currentHoverValue = ref(0);

const handleNpsMove = (val: number) => {
  if (disabledProp.value) {
    return;
  }
  setCurrentHoverValue(val);
};

const setCurrentHoverValue = (val: number) => {
  if (val < changeValue.value) {
    return;
  }
  currentHoverValue.value = val;
};
const handleClick = (val: number) => {
  if (disabledProp.value) {
    return;
  }
  currentHoverValue.value = val;
  changeValue.value = Number(val);
};

const createArray = (start: number, end: number) => {
  let array = [];
  if (props.table?.reverseOrder) {
    for (let i = end; i >= start; i--) {
      array.push(i);
    }
  } else {
    for (let i = start; i <= end; i++) {
      array.push(i);
    }
  }
  return array;
};

console.log(document.querySelector(".t-nps-wrap"));
// 生成颜色列表 获取主题色
const themeColor = ref("#4c4edb");

onMounted(() => {
  const dom = document.querySelector(".t-form-theme-wrap");
  if (dom) {
    themeColor.value = getComputedStyle(dom).getPropertyValue(`--form-theme-color`);
  }
  if (props.value) {
    currentHoverValue.value = props.value;
  }
});

/**
 * 生成从浅到深的颜色列表
 * @param baseColor 基础颜色（HEX、RGB、HSL 都可以）
 * @param count 颜色数量（>=2）
 * @returns 颜色数组（HEX 字符串）
 */
const generateShades = computed(() => {
  const color = new TinyColor(toValue(themeColor));
  const count = props.table?.level;
  const pairs: { background: string; text: string }[] = [];

  const maxLighten = 40;
  const step = maxLighten / (count - 1);

  for (let i = count - 1; i >= 0; i--) {
    const lightPercent = step * i;
    const bgColor = color.clone().lighten(lightPercent);
    const textColor = bgColor.isLight() ? "#000000" : "#ffffff"; // 根据背景选文字色

    pairs.push({
      background: bgColor.toHexString(),
      text: textColor
    });
  }

  return pairs;
});

const getHoverStyle = (index: number) => {
  if (currentHoverValue.value < index) {
    return {};
  }
  const colorIndex = index - 1;
  return {
    backgroundColor: generateShades.value[colorIndex].background,
    color: generateShades.value[colorIndex].text
  };
};
</script>

<style lang="scss" scoped>
.t-nps-wrap {
  --form-theme-color: var(--form-theme-color, var(--el-color-primary));
}

.tip-text {
  font-size: var(--el-font-size-base);
  color: var(--el-text-color-regular);
  margin-top: 5px;
  display: flex;
  justify-content: space-between;
  position: relative;

  .middle-text {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
  }
}

.t-nps-box {
  display: flex;
  align-items: center;
  width: 100%;
  height: var(--el-component-size);

  .item {
    transition: all 0.3s ease;
    height: var(--el-component-size);
    line-height: var(--el-component-size);
    flex: 1;
    text-align: center;
    display: inline-block;
    color: #314666;
    border: 1px solid rgba(0, 0, 0, 0.06);
    border-radius: 4px;
    margin-right: 8px;
    cursor: pointer;
  }

  .active {
    background-color: var(--form-theme-color, var(--el-color-primary));
    color: #fff;
  }
}

/*// 变亮
*/
@function tint($color, $percentage) {
  @return mix(white, $color, $percentage);
}
</style>
